<template>
	<view>
		<!-- 搜索框 -->
		<view class="search_box">
			<!-- 使用 uni-ui 提供的搜索组件 -->
			<uni-search-bar @input="input" cancelButton="none" radius="20" :focus="true"></uni-search-bar>
		</view>
		<!-- 搜索历史 -->
		<view class="history_container" v-if="searchResults.length ==0">
			<!-- 搜索title -->
			<view class="history_title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="18" @click="cleanHistory"></uni-icons>
			</view>
			<!-- 历史搜索关键词 -->
			<view class="history_keywords">
				<uni-tag :text="item" v-for="(item,index) in historyKeywords" :key="index" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>

		<!-- 搜索结果列表 -->
		<view class="search_results_list" v-else>
			<view class="search_results_item" v-for="(item, i) in searchResults" :key="i"
				@click="gotoDetail(item.goods_id)">
				<view class="search_results_item_name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchValue: '',
				timerId: null, // 延时器的 timerId
				searchResults: [], //搜索后结果
				historyKeywords: [] //历史数据
			}
		},
		methods: {
			input(data) {
				clearTimeout(this.timerId)
				this.timerId = setTimeout(() => {
					if (!data.trim()) return this.searchResults = []
					this.searchValue = data
					this.getSearchResults()
				}, 500)
			},
			// 获取搜索后结果
			async getSearchResults() {
				let {
					data
				} = await uni.$http.get('/goods/qsearch', {
					query: this.searchValue
				})
				if (data.meta.status !== 200) return uni.$showMsg()
				this.searchResults = data.message
				this.saveHistoryKeywords()
			},
			// 跳转到商品详情
			gotoDetail(data) {
				uni.navigateTo({
					// 指定详情页面的 URL 地址，并传递 goods_id 参数
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + data
				})
			},
			// 保存历史数据
			saveHistoryKeywords() {
				const index = this.historyKeywords.indexOf(this.searchValue)
				if (index >= 0) this.historyKeywords.splice(index, 1)
				this.historyKeywords.unshift(this.searchValue)
				// 保存数据到本地
				uni.setStorageSync('historyKeywords', JSON.stringify(this.historyKeywords))
			},
			// 清空历史数据
			cleanHistory(){
				this.historyKeywords = []
				uni.setStorageSync('historyKeywords', '[]')
			},
			// 跳转到商品列表页
			gotoGoodsList(item){
				uni.navigateTo({
					// 指定详情页面的 URL 地址，并传递 goods_id 参数
					url: '/subpkg/goods_list/goods_list?query=' + item
				})
			}
		},
		onLoad() {
			this.historyKeywords = JSON.parse(uni.getStorageSync('historyKeywords') || '[]')
		}
	}
</script>

<style lang="scss">
	.search_box {
		background-color: #c00000;
		position: sticky;
		top: 0;
		z-index: 9;
	}

	.search_results_list {
		padding: 0 20rpx;

		.search_results_item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1rpx solid #efefef;
			height: 90rpx;

			.search_results_item_name {
				font-size: 24rpx;
				// 文字不允许换行（单行文本）
				white-space: nowrap;
				// 溢出部分隐藏
				overflow: hidden;
				// 文本溢出后，使用 ... 代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history_container {
		.history_title {
			display: flex;
			height: 80rpx;
			justify-content: space-between;
			align-items: center;
			padding: 0 20rpx;
			border-bottom: 1rpx solid #efefef;
		}

		.history_keywords {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				margin: 10rpx;
			}
		}
	}
</style>